<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/16 0016
 * Time: 下午 15:09
 */
class mypagerHelper
{
    public $total;
    public $perPage;
    public $table;
    public $pageCount;
    public $curPage = 1;
    public $link;
    public $maxButtonCount = 10;
    public $limit;
    public $start = 0;

    public function __construct($db,$table,$link,$page=1,$where='',$perPage = 20)
    {
        $this->table = $table;
        $this->perPage = $perPage;
        $this->total = $db->getCount($table,$where);
        $this->link = $link;
        $this->pageCount = intval(ceil($this->total / (float)$perPage));
        $this->curPage = $page > $this->pageCount ? 1 : $page;
        $start = ($this->curPage - 1) * $this->perPage;
        $this->start = $start;
        $this->limit = ' LIMIT '.$start.','.$perPage;
    }


    /**
     * Executes the widget.
     * This overrides the parent implementation by displaying the generated page buttons.
     */
    public function run()
    {
        echo $this->renderPageButtons();
    }

    public function getPageList()
    {
        return $this->renderPageButtons();
    }

    public function getPageCount()
    {
        return $this->pageCount;
    }

    /**
     * Renders the page buttons.
     * @return string the rendering result
     */
    protected function renderPageButtons()
    {
        $pageCount = $this->pageCount;
        if ($this->pageCount < 2) {
            return '';
        }
        $buttons = array();
        $currentPage = $this->curPage;

        // first page
        $buttons[] = $this->renderPageButton('首页', 1, $currentPage <= 1, false);

        // prev page
            if (($page = $currentPage - 1) <= 0) {
                $page = 1;
            }
            $buttons[] = $this->renderPageButton('上一页', $page, $currentPage <= 1, false);

        // internal pages
        list($beginPage, $endPage) = $this->getPageRange();
        for ($i = $beginPage; $i <= $endPage; ++$i) {
            $buttons[] = $this->renderPageButton($i , $i, null,  $i == $currentPage);
        }

        // next page

            if (($page = $currentPage ) >= $pageCount) {
                $page = $pageCount;
            }
            $buttons[] = $this->renderPageButton('下一页', $page,  $currentPage >= $pageCount, false);


        // last page
            $buttons[] = $this->renderPageButton('尾页', $pageCount, $currentPage >= $pageCount, false);

        return  implode("\n", $buttons);
    }

    /**
     * Renders a page button.
     * You may override this method to customize the generation of page buttons.
     * @param string $label the text label for the button
     * @param integer $page the page number
     * @param string $class the CSS class for the page button.
     * @param boolean $disabled whether this page button is disabled
     * @param boolean $active whether this page button is active
     * @return string the rendering result
     */

    //<span>上一页</span> <span class="current">1</span>
    //<a href="">2</a><a href="">3</a><a href="">下一页</a><a href="">尾页</a>
    protected function renderPageButton($label, $page, $disabled, $active)
    {
        if ($active) {
            return '<span class="current">'.$label.'</span>';
        }
        if ($disabled) {
            return '<span class="current">'.$label.'</span>';
        }
        return '<a href="'.$this->link.',page,'.$page.'">'.$label.'</a>';
    }

    /**
     * @return array the begin and end pages that need to be displayed.
     */
    protected function getPageRange()
    {
        $currentPage = $this->curPage;
        $pageCount = $this->pageCount;

        $beginPage = max(1, $currentPage - (int) ($this->maxButtonCount / 2));
        if (($endPage = $beginPage + $this->maxButtonCount) >= $pageCount) {
            $endPage = $pageCount;
            $beginPage = max(1, $endPage - $this->maxButtonCount);
        }
        return array($beginPage, $endPage);
    }

}